# ----------------------------------------------------------------------
#  Paper: Party Competition and Cooperation Shape Affective Polarization
#  Last updated: February 2021
#  Purpose: Analyze INES data (Study I)
#  Outputs: All tables and figures for Study I
#  Machine: Chagai's macbook pro
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# load all relevant themes and packages
# ----------------------------------------------------------------------
rm(list=ls())
library("tidyverse")
library("ggplot2")
library("stargazer")
library("xtable")
library("texreg")
library("readstata13")
library("estimatr")
library("naniar")
library("lfe")
library("multiwayvcov")
library("lmtest")

#require(devtools)
#install_version("dplyr", version = "0.8.5", repos = "http://cran.us.r-project.org")

### Read data
ines_master <- read.csv("Data for analysis/ines_master.csv")

# Create treatment variable -- proximity to elections
ines_master$treatment <- 45 - ines_master$days_before_elec


##############################################
# Create Table 1
##############################################

# Run base model
base_polar <- felm(party_polar ~ treatment | election_year | 0 |election_year,
                   data = ines_master,
                   keepCX = TRUE) 
summary(base_polar)

# add demog
polar_demo <- felm(party_polar ~ treatment + age +
                     Sex + ashkenazi | election_year | 0 |election_year,
                   data = ines_master,
                   keepCX = TRUE)
summary(polar_demo)


# add social
polar_demo_soc <- felm(party_polar ~ treatment + age +
                         Sex + ashkenazi + relig + edu
                       | election_year | 0 |election_year,
                       data = ines_master,
                       keepCX = TRUE)
summary(polar_demo_soc)

# in-party and out party affect
in_party_affect <- felm(in_party_affect ~ treatment  + age +
                          Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                        data = ines_master,
                        keepCX = TRUE)
summary(in_party_affect)


out_party_affect <- felm(party_affect ~ treatment  + age +
                           Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                         data = ines_master,
                         keepCX = TRUE)
summary(out_party_affect)


## Create Table  
stargazer(base_polar, polar_demo, polar_demo_soc, 
          in_party_affect,out_party_affect,
          out= "Tables/main_election.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization and Party Affect",
          keep = "treatment",
          dep.var.labels = c("Polarization", 
                             "In-Party Affect", "Out-Party Affect"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:main_elec",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "Yes", "Yes", "Yes", "Yes"),
            c("Social Controls",  "No", "No", "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes" ),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes")))


##############################################
# Create Table 2
##############################################

### Knowledge

knowl_1 <- felm(knowledge ~ treatment 
                | election_year | 0 |election_year,
                data = ines_master,
                exactDOF=TRUE,
                keepCX = TRUE) 
summary(knowl_1)


knowl_3 <- felm(knowledge ~ treatment  + age +
                  Sex + ashkenazi + relig + edu
                | election_year | 0 |election_year,
                data = ines_master,
                exactDOF=TRUE,
                keepCX = TRUE) 
summary(knowl_3)


### Engagement

conv1 <- felm(politc_conv ~ treatment | election_year | 0 |election_year,
              data = ines_master,
              exactDOF=TRUE,
              keepCX = TRUE) 
summary(conv1)


conv3 <- felm(politc_conv ~ treatment  + age +
                Sex + ashkenazi + relig + edu
              | election_year | 0 |election_year,
              data = ines_master,
              exactDOF=TRUE,
              keepCX = TRUE) 
summary(conv3)

# Participation

turn1 <- felm(turnout ~ treatment | election_year | 0 |election_year,
              data = ines_master,
              exactDOF=TRUE,
              keepCX = TRUE) 
summary(turn1)

turn3 <- felm(turnout ~ treatment  + age +
                Sex + ashkenazi + relig + edu
              | election_year | 0 |election_year,
              data = ines_master,
              exactDOF=TRUE,
              keepCX = TRUE) 
summary(turn3)

stargazer(knowl_1, knowl_3,
          conv1, conv3,
          turn1, turn3,
          out= "Tables/mech_main.tex",  style="qje",
          title="Potential Mechanisms",
          keep = c("treatment"),
          dep.var.labels = c("Information", "Engagment", "Turnout"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:mech_main",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "Yes", "No", "Yes", "No", "Yes"),
            c("Social Controls",  "No", "Yes", "No", "Yes", "No", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")))



##############################################
# Create Figure 1
##############################################

ines_master <- ines_master %>% 
  mutate(.,
         treat_categ = case_when
         (treatment %in% 0:9 ~ 1,
           treatment %in% 10:19 ~ 2,
           treatment %in% 20:29 ~ 3,
           treatment %in% 30:44 ~ 4))

ines_master$treat_categ <- factor(ines_master$treat_categ, levels = c(1,2,3,4))
time_dummies <- felm(party_polar ~ as.factor(treat_categ)
                     | election_year | 0 |election_year,
                     data = ines_master,
                     exactDOF=TRUE,
                     keepCX = TRUE) 
summary(time_dummies)

time_dummies_tidy <- tidy(time_dummies, conf.int = T) %>% 
  mutate(.,
         Model = "No Controls",
         term = case_when(
           term == "as.factor(treat_categ)2" ~ "26-35 Days \nBefore Election",
           term == "as.factor(treat_categ)3" ~ "16-25 Days \nBefore Election",
           term == "as.factor(treat_categ)4" ~ "1-15 Days \nBefore Election"
         ))%>% 
  filter(.,
         !is.na(term))

time_dummie_cov <- felm(party_polar ~ as.factor(treat_categ) + age +
                          Sex + ashkenazi  + relig + edu
                        | election_year | 0 |election_year,
                        data = ines_master,
                        exactDOF=TRUE,
                        keepCX = TRUE) 
summary(time_dummie_cov)
time_dummie_cov_tidy <- tidy(time_dummie_cov, conf.int = T)  %>% 
  mutate(.,
         Model = "Controls",
         term = case_when(
           term == "as.factor(treat_categ)2" ~ "26-35 Days \nBefore Election",
           term == "as.factor(treat_categ)3" ~ "16-25 Days \nBefore Election",
           term == "as.factor(treat_categ)4" ~ "1-15 Days \nBefore Election"
         )) %>% 
  filter(.,
         !is.na(term))



dummy_effect <- rbind(time_dummies_tidy, time_dummie_cov_tidy)

order_x <- c("26-35 Days \nBefore Election",
             "16-25 Days \nBefore Election", 
             "1-15 Days \nBefore Election")

dummy_effect$Model <- factor(dummy_effect$Model, 
                             levels = c("No Controls", "Controls"))
# Generate coefficient plot 
ggplot(dummy_effect, aes(x = term, y = estimate, color = Model)) +
  geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high,
                      color = Model, shape = Model),
                  position = position_dodge(width = 0.4),
                  fill = "white") +
  #coord_flip()+
  labs(x = "",
       y = "Effect Size") +
  scale_x_discrete(limits= order_x) +
  scale_color_manual(values = c("dodgerblue2", "firebrick2")) +
  scale_fill_manual(values = c("dodgerblue2", "firebrick2")) +
  # caption = "*Coefficients from OLS models, in which outcomes are standardized."
  theme(text = element_text(size = 10, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(), 
        axis.text.x = element_text(size = 10),
        plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))
ggsave("Figures/time_dummy.pdf", width = 6.5, height = 4)


##############################################
# Create figures and tables for Appendix
##############################################

##############################################
# Create Figure A1
##############################################

# Create df of mean treatment per election year
s <- group_by(ines_master, election_year)

s <- summarize(s, 
               n = n(),
               mean_treat = mean(days_before_elec, na.rm = TRUE),
               sd_treat = sd(days_before_elec, na.rm = TRUE))


# Plot treatment
ggplot(ines_master, aes(days_before_elec, fill = as.factor(election_year))) + 
  geom_bar() + 
  labs(x = "Days Before Election",
       y = "Number of Observations",
       fill = "Election Year",
       color = "Election Year") +
  scale_color_manual(values = c("#000000", "#E69F00", "#56B4E9", "#009E73",
                                "#F0E442", "#0072B2", "#D55E00", "#CC79A7")) +
  scale_fill_manual(values = c("#000000", "#E69F00", "#56B4E9", "#009E73",
                               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")) +
  # caption = "*Coefficients from OLS models, in which outcomes are standardized."
  theme(text = element_text(size = 10, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(), 
        axis.text.x = element_text(size = 8),
        plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))
ggsave("Figures/treatment1.pdf", width = 6.5, height = 4)


##############################################
# Create Table A1
##############################################

ines_master <- ines_master %>% 
  mutate(.,
         Secular = ifelse(relig == "Secular",1,0),
         Traditional = ifelse(relig == "Traditional", 1,0),
         Religious = ifelse(relig == "Relig", 1,0),
         V_Religous = ifelse(relig == "V. Relig", 1,0),
         Spending_WBA = ifelse(spending == 1, 1,0),
         Spending_BA = ifelse(spending == 2, 1,0),
         Spending_A = ifelse(spending == 3, 1,0),
         Spending_AA = ifelse(spending == 4, 1,0),
         Spending_WAA = ifelse(spending == 5, 1,0),
         Academic = ifelse(edu == "Academic", 1, 0),
         HS = ifelse(edu == "HS", 1, 0),
         Less_hs = ifelse(edu == "Less then HS", 1, 0))

ines_master <- ines_master %>% 
  mutate(.,
         female = ifelse(Sex == "male", 0, 1))

disc_table <- select(ines_master,
                     c(age, female, Secular, Traditional, Religious,
                       V_Religous, Spending_WBA, Spending_BA, Spending_A,
                       Spending_AA, Spending_WAA, 
                       Less_hs, HS, Academic, ashkenazi,
                       party_polar, party_polar1,
                       party_affect, in_party_affect,
                       knowledge, turnout, politc_conv))

stargazer(as.data.frame(disc_table),
          covariate.labels = c("Age",  "Female", "Secular", "Traditional", "Religious",
                               "Very Religous", "Spending Well Below Average",
                               "Spending Below Average", "Average Spending",
                               "Above Average Spending", "Well Above Average Spending",
                               "Education - Less than HS", "Education - HS",
                               "Education - Academic", "Ashkenazi",
                               "Party Polarization", "Party Polarization (Alterantive)",
                               "Out-Party Affect", "In-Party Affect",
                               "Knowledge", "Intended Participation", "Engagment"),
          title = "Descriptive Statistics - Jewish Survey Respondents (2001-2019)",
          label = "tab:dsc",
          style = "qje",
          notes = c("Spending Variable refers to Household spending."),
          notes.append = T,
          notes.align = "l",
          out="Tables/dscrpt.tex")


##############################################
# Create Table A2
##############################################

y <- group_by(ines_master, election_year)
year_obs <- summarize(y, 
                      "Obs" = n(),
                      "Mean Treatment" = mean(treatment, na.rm = TRUE),
                      "SD Treatment" = sd(treatment, na.rm = TRUE),
                      "Mean Polarization" = mean(party_polar, na.rm = TRUE),
                      "SD Polarization" = sd(party_polar, na.rm = TRUE)) %>% 
  mutate(.,
         election_year = case_when(
           election_year == 2001 ~ "2001",
           election_year == 2003 ~ "2003",
           election_year == 2006 ~ "2006",
           election_year == 2009 ~ "2009",
           election_year == 2013 ~ "2013",
           election_year == 2015 ~ "2015",
           election_year == 2019 ~ "2019")) %>% 
  rename(.,
         "Cycle" = election_year) 
year_obs <-  as.data.frame(year_obs)
stargazer(year_obs, 
          summary = FALSE, rownames = TRUE, 
          title = "Observations, Treatment, and Outcome By Election Cycle",
          label = "tab:elec",
          style = "qje",
          notes.append = T,
          notes.align = "l",
          out="Tables/elec_obs.tex")

#Table A3 does not rely on data
##############################################
# Create Table A4            
##############################################

rw1 <- felm(right_wing ~ treatment | election_year | 0 |election_year,
            data = ines_master,
            exactDOF=TRUE,
            keepCX = TRUE) 
summary(rw1)

rw2 <- felm(right_wing ~ treatment  + age +
              Sex + ashkenazi
            | election_year | 0 |election_year,
            data = ines_master,
            exactDOF=TRUE,
            keepCX = TRUE) 
summary(rw2)


rw3 <- felm(right_wing ~ treatment  + age +
              Sex + ashkenazi  + relig + edu
            | election_year | 0 |election_year,
            data = ines_master,
            exactDOF=TRUE,
            keepCX = TRUE) 
summary(rw3)

# Table for main text

stargazer(rw1, rw2, rw3,
          out= "Tables/rw_identification.tex",  style="qje",
          title="Effects of Treatment on Right Wing Identification",
          keep = c("treatment"),
          dep.var.labels = c("Right Wing"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=3,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:rw_id",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "Yes", "Yes"),
            c("Social Controls",  "No", "No", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))

##############################################
# Create Figure A2             
##############################################

# Bivariate models of all demographics
age <- felm(treatment ~ age | election_year | 0 |election_year,
            data = ines_master,
            keepCX = TRUE) 

age_tidy <- tidy(age, conf.int = T) %>% 
  mutate(.,
         term = "Age",
         Model = "Bivariate")

sex <- felm(treatment ~ Sex | election_year | 0 |election_year,
            data = ines_master,
            keepCX = TRUE) 

sex_tidy <- tidy(sex, conf.int = T) %>% 
  mutate(.,
         term = "Female",
         Model = "Bivariate")


secul <- felm(treatment ~ Secular | election_year | 0 |election_year,
              data = ines_master,
              keepCX = TRUE) 

secul_tidy <- tidy(secul, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate")

tradit <- felm(treatment ~ Traditional | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

tradit_tidy <- tidy(tradit, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate")


religious <- felm(treatment ~ Religious | election_year | 0 |election_year,
                  data = ines_master,
                  keepCX = TRUE) 

religious_tidy <- tidy(religious, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate")

v_relig <- felm(treatment ~ V_Religous | election_year | 0 |election_year,
                data = ines_master,
                keepCX = TRUE) 

v_relig_tidy <- tidy(v_relig, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Very Religious")


spend1 <- felm(treatment ~ Spending_WBA | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

spend1_tidy <- tidy(spend1, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Well Below Average Spending")


spend2 <- felm(treatment ~ Spending_BA | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

spend2_tidy <- tidy(spend2, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Below Average Spending")


spend3 <- felm(treatment ~ Spending_A | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

spend3_tidy <- tidy(spend3, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Average Spending")

spend4 <- felm(treatment ~ Spending_AA | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

spend4_tidy <- tidy(spend4, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Above Average Spending")


spend5 <- felm(treatment ~ Spending_WAA | election_year | 0 |election_year,
               data = ines_master,
               keepCX = TRUE) 

spend5_tidy <- tidy(spend5, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Well Above Average Spending")

edu1 <- felm(treatment ~ Less_hs | election_year | 0 |election_year,
             data = ines_master,
             keepCX = TRUE) 

edu1_tidy <- tidy(edu1, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Less Than High-School")

edu2 <- felm(treatment ~ HS | election_year | 0 |election_year,
             data = ines_master,
             keepCX = TRUE) 

edu2_tidy <- tidy(edu2, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "High-School")

edu3 <- felm(treatment ~ Academic | election_year | 0 |election_year,
             data = ines_master,
             keepCX = TRUE) 

edu3_tidy <- tidy(edu3, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Academic")

ashkenazi <- felm(treatment ~ ashkenazi | election_year | 0 |election_year,
                  data = ines_master,
                  keepCX = TRUE) 

ashkenazi_tidy <- tidy(ashkenazi, conf.int = T) %>% 
  mutate(.,
         Model = "Bivariate",
         term = "Ashkenazi")



multivar <- felm(treatment ~ age + edu + as.factor(spending) +
                   relig + Sex + ashkenazi
                 | election_year | 0 |election_year,
                 data = ines_master,
                 keepCX = TRUE)

multivar_tidy <- tidy(multivar, conf.int = T) %>% 
  mutate(.,
         term = case_when(
           term == "age" ~ "Age",
           term == "eduHS" ~ "High-School",
           term == "eduLess then HS" ~ "Less Than High-School",
           term == "as.factor(spending)2" ~ "Below Average Spending",
           term == "as.factor(spending)3" ~ "Average Spending",
           term == "as.factor(spending)4" ~ "Above Average Spending",
           term == "as.factor(spending)5" ~ "Well Above Average Spending",
           term == "religSecular" ~ "Secular",
           term == "religTraditional" ~ "Traditional",
           term == "religV. Relig" ~ "Very Religious",
           term == "Sexfemale" ~ "Female",
           term == "ashkenazi" ~ "Ashkenazi"),
         Model = "Multivariate")

# Run Model without Spending
multivar2 <- felm(treatment ~ age + edu +
                    relig + Sex + ashkenazi
                  | election_year | 0 |election_year,
                  data = ines_master,
                  keepCX = TRUE)
summary(multivar2)
multivar2_tidy <- tidy(multivar, conf.int = T) %>% 
  mutate(.,
         term = case_when(
           term == "age" ~ "Age",
           term == "eduHS" ~ "High-School",
           term == "eduLess then HS" ~ "Less Than High-School",
           term == "religSecular" ~ "Secular",
           term == "religTraditional" ~ "Traditional",
           term == "religV. Relig" ~ "Very Religious",
           term == "Sexfemale" ~ "Female",
           term == "ashkenazi" ~ "Ashkenazi"),
         Model = "Multivariate")

bal <- rbind(age_tidy, sex_tidy, secul_tidy, tradit_tidy, 
             religious_tidy, v_relig_tidy, spend1_tidy,
             spend2_tidy, spend3_tidy, spend4_tidy, spend5_tidy, 
             edu1_tidy, edu2_tidy, edu3_tidy,
             ashkenazi_tidy, multivar_tidy)

order_x <- c("Age", "Female", "Secular", 
             "Traditional", "Religious", "Very Religious",
             "Well Below Average Spending", "Below Average Spending",
             "Average Spending", "Above Average Spending", 
             "Well Above Average Spending", "Less Than High-School",
             "High-School", "Academic", "Ashkenazi") %>% rev(.)
ggplot(bal, aes(x = term, y = estimate)) +
  geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high, color = Model, shape = Model),
                  position = position_dodge(width = 0.4),
                  fill = "white") +
  coord_flip()+
  scale_color_manual(values = c("firebrick2", "dodgerblue2")) +
  scale_fill_manual(values = c("firebrick3", "dodgerblue3")) +
  labs(x = "",
       y = "Effect Size") +
  scale_x_discrete(limits= order_x) +
  # caption = "*Coefficients from OLS models, in which outcomes are standardized."
  theme(text = element_text(size = 10, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(), 
        axis.text.x = element_text(size = 8),
        plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))
ggsave("Figures/balance.pdf", width = 6.5, height = 4)

##############################################
# Create Table A5             
##############################################

stargazer(multivar, multivar2,
          out= "Tables/balance.tex",  style="qje",
          title="Demographic Correlations with Treatment",
          dep.var.labels = c("Proximity to Election"),
          covariate.labels = c("Age", "High-School", "Less than High-School",
                               "Below Average Spending", "Average Spending",
                               "Above Average Spending", "Well Above Average Spending",
                               "Secular", "Traditional", "Very Religious",
                               "Female", "Ashkenazi"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          no.space = T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          font.size = "small",
          label = "tab:bal",
          star.char = c("", "", ""),
          add.lines = list(
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes" ),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes")))


##############################################
# Create Figure A3
##############################################

ines_master$day_name <- weekdays(as.Date(ines_master$date))


# Plot responses by day of the week
days <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
          "Saturday")
ggplot(ines_master, aes(day_name, fill = as.factor(election_year),
                        color = NA)) + 
  geom_bar(position = "stack") + 
  scale_color_manual(values = c("#000000", "#E69F00", "#56B4E9", "#009E73",
                                "#F0E442", "#0072B2", "#D55E00", "#CC79A7")) +
  scale_fill_manual(values = c("#000000", "#E69F00", "#56B4E9", "#009E73",
                               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")) +
  labs(x = "Day of the Week",
       y = "Number of Observations",
       fill = "Election Year",
       color = "Election Year") +
  scale_x_discrete(limits= days) +
  # caption = "*Coefficients from OLS models, in which outcomes are standardized."
  theme(text = element_text(size = 10, family = "Times"),
        legend.key=element_blank(),
        panel.grid.major = element_blank(), 
        axis.text.x = element_text(size = 8),
        plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))
ggsave("Figures/days.pdf", width = 6.5, height = 4)

##############################################
# Create Table A6
##############################################

ines_master <- ines_master %>% 
  mutate(.,
         day_of_week = case_when(
           day_name == "Sunday" ~ 1,
           day_name == "Monday" ~ 2,
           day_name == "Tuesday" ~ 3,
           day_name == "Wednesday" ~ 4,
           day_name == "Thursday" ~ 5,
           day_name == "Friday" ~ 6
         ),
         Sunday = ifelse(day_name == "Sunday", 1,0))

# Day of the week correlation with demographics

day_multivar <- felm(day_of_week ~ age + edu + as.factor(spending) +
                       relig + Sex + ashkenazi
                     | election_year | 0 |election_year,
                     data = ines_master,
                     keepCX = TRUE)
summary(day_multivar)

# Run Model without Spending
day_multivar2 <- felm(day_of_week ~ age + edu +
                        relig + Sex + ashkenazi
                      | election_year | 0 |election_year,
                      data = ines_master,
                      keepCX = TRUE)
summary(day_multivar2)


# generate table
stargazer(day_multivar, day_multivar2,
          out= "Tables/days_demog.tex",  style="qje",
          title="Demographic Correlations with Day of the Week",
          dep.var.labels = c("Days of Week (Sunday - Saturday)"),
          covariate.labels = c("Age", "High-School", "Less than High-School",
                               "Below Average Spending", "Average Spending",
                               "Above Average Spending", "Well Above Average Spending",
                               "Secular", "Traditional", "Very Religious",
                               "Female", "Ashkenazi"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          no.space = T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          font.size = "small",
          label = "tab:days_bal",
          star.char = c("", "", ""),
          add.lines = list(
            c("Year FEs", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes")))


##############################################
# Create Table A7
##############################################

log_polar <- felm(party_polar ~ log(treatment) | election_year | 0 |election_year,
                  data = ines_master,
                  keepCX = TRUE) 
summary(log_polar)

log_polar1 <- felm(party_polar ~ log(treatment)  + age +
                     Sex + ashkenazi  + relig + edu| election_year | 0 |election_year,
                   data = ines_master,
                   keepCX = TRUE) 
summary(log_polar1)


polar_prt <- felm(party_polar1 ~ treatment | election_year | 0 |election_year,
                  data = ines_master,
                  keepCX = TRUE) 
summary(polar_prt)

polar_prt1 <- felm(party_polar1 ~ treatment + age +
                     Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                   data = ines_master,
                   keepCX = TRUE) 
summary(polar_prt1)


stargazer(log_polar, log_polar1,
          polar_prt, polar_prt1,
          out= "Tables/robust_elec_meas.tex",  style="qje",
          title="Election Effect - Robustness to Alternative Measurment",
          keep = c("treatment", "log(treatment)"),
          dep.var.labels = c("Polarization", "Polarization"),
          column.labels = c("Log","Log",
                            "Partisans", "Partisans"),
          covariate.labels = c("Log Proximity to Election",
                               "Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:main_elec_rob",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "Yes", "No", "Yes"),
            c("Social Controls",  "No", "Yes", "No", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes")))


##############################################
# Create Table A8
##############################################

polar2001 <- ines_master %>% 
  filter(.,
         election_year !=2001) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2001)

polar2003 <- ines_master %>% 
  filter(.,
         election_year !=2003) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2003)


polar2006 <- ines_master %>% 
  filter(.,
         election_year !=2006) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2006)


polar2009 <- ines_master %>% 
  filter(.,
         election_year !=2009) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2009)


polar2013 <- ines_master %>% 
  filter(.,
         election_year !=2013) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2013)

polar2015 <- ines_master %>% 
  filter(.,
         election_year !=2015) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2015)


polar2019 <- ines_master %>% 
  filter(.,
         election_year !=2019) %>% 
  felm(party_polar ~ treatment | election_year | 0 |election_year,
       data = .,
       keepCX = TRUE) 
summary(polar2019)

stargazer(polar2001,
          polar2003, polar2006, polar2009,
          polar2013, polar2015, polar2019,
          out= "Tables/robust_elec_years.tex",  style="qje",
          title="Election Effect - Robustness to Cycle Ommisions",
          #     omit = c(2:8),
          dep.var.labels = c("Polarization"),
          column.labels = c("Omit 01","Omit 03",
                            "Omit 06", "Omit 09", "Omit 13",
                            "Omit 15", "Omit 19"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:rob_year",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "No", "No", "No", "No", "No","No"),
            c("Social Controls",  "No", "No", "No", "No", "No", "No","No"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes","Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes","Yes")))


##############################################
# Create Table A9
##############################################
# Robustness to considering day of the week

day_contin <- felm(party_polar ~ treatment*day_of_week
                   | election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(day_contin)


sunday <- felm(party_polar ~ treatment*Sunday
               | election_year | 0 |election_year,
               data = ines_master,
               exactDOF=TRUE,
               keepCX = TRUE) 
summary(sunday)


day_dummy <- felm(party_polar ~ treatment + day_name
                  | election_year | 0 |election_year,
                  data = ines_master,
                  exactDOF=TRUE,
                  keepCX = TRUE) 
summary(day_dummy)

stargazer(day_contin, sunday, day_dummy,
          out= "Tables/days.tex",  style="qje",
          title="Robustness to Day of the Week",
          #keep = c("treatment", "log(treatment)"),
          dep.var.labels = c("Affective Polarization"),
          covariate.labels = c("Proximity to Election", "Day of Week",
                               "Proximity to Election * Day of Week",
                               "Sunday", "Proximity to Election * Sunday", "Monday",
                               "Sunday", "Thursday", "Tuesday", "Wednesday"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:days",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "No", "No"),
            c("Social Controls",  "No", "No", "No"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))


##############################################
# Create Table A10
##############################################

main_party1 <- felm(main_party_polar ~ treatment | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(main_party1)

main_party2 <- felm(main_party_polar ~ treatment  + age +
                      Sex + ashkenazi
                    | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(main_party2)


main_party3 <- felm(main_party_polar ~ treatment  + age +
                      Sex + ashkenazi + relig + edu
                    | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(main_party3)


ext_party1 <- felm(extr_party_polar ~ treatment | election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(ext_party1)


ext_party2 <- felm(extr_party_polar ~ treatment  + age +
                     Sex + ashkenazi
                   | election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(ext_party2)


ext_party3 <- felm(extr_party_polar ~ treatment  + age +
                     Sex + ashkenazi + relig + edu
                   | election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(ext_party3)


# Create table
stargazer(main_party1, main_party2, main_party3,
          ext_party1, ext_party2, ext_party3,
          out= "Tables/rob_two_party_affect.tex",  style="qje",
          title="Robustness Check - Competition Effects on Center and Non-Center Party Polarization",
          keep = c("treatment"),
          dep.var.labels = c("Center Party Polarization", "Non-Center Party Polarization"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=3,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:rob_main_party",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "Yes", "Yes", "No", "Yes", "Yes"),
            c("Social Controls",  "No", "No", "Yes", "No", "No", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")))



##############################################
# Create Table A11
##############################################

#### Models without Clusters at the year level

# Run base model
polar_no_clus <- felm(party_polar ~ treatment | election_year | 0 |0,
                      data = ines_master,
                      keepCX = TRUE) 
summary(polar_no_clus)

# add demog
polar_demo_no_clus <- felm(party_polar ~ treatment + age +
                             Sex + ashkenazi | election_year | 0 |0,
                           data = ines_master,
                           keepCX = TRUE)
summary(polar_demo_no_clus)


# add social
polar_demo_soc_no_clus <- felm(party_polar ~ treatment + age +
                                 Sex + ashkenazi + relig + edu
                               | election_year | 0 |0,
                               data = ines_master,
                               keepCX = TRUE)
summary(polar_demo_soc_no_clus)

in_party_affect_no_clus <- felm(in_party_affect ~ treatment | election_year | 0 |0,
                                data = ines_master,
                                keepCX = TRUE)
summary(in_party_affect_no_clus)


out_party_affect_no_clus <- felm(party_affect ~ treatment | election_year | 0 |election_year,
                                 data = ines_master,
                                 keepCX = TRUE)
summary(out_party_affect_no_clus)

## Create Table  
stargazer(polar_no_clus, polar_demo_no_clus, polar_demo_soc_no_clus, 
          in_party_affect_no_clus, out_party_affect_no_clus,
          out= "Tables/main_election_no_clus.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization and Party Affect Without Year Clusters",
          keep = "treatment",
          dep.var.labels = c("Polarization", 
                             "In-Party Affect", "Out-Party Affect"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:elec_no_clus",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "Yes", "Yes", "No", "No"),
            c("Social Controls",  "No", "No", "Yes", "No", "No"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes" ),
            c("Year Cluster",  "No", "No", "No", "No", "No")))

##############################################
# Create Table A12
##############################################
#### Models with election week cluster

# Run base model
base_week_clus <- felm(party_polar ~ treatment |
                         election_year | 0 |election_year + week,
                       data = ines_master,
                       keepCX = TRUE) 
summary(base_week_clus)


# add demog
demo_week_clus <- felm(party_polar ~ treatment + age +
                         Sex + ashkenazi | 
                         election_year | 0 |election_year + week,
                       data = ines_master,
                       keepCX = TRUE)
summary(demo_week_clus)


# add social
social_week_clus <- felm(party_polar ~ treatment + age +
                           Sex + ashkenazi + relig + edu
                         | election_year | 0 |election_year + week,
                         data = ines_master,
                         keepCX = TRUE)
summary(social_week_clus)


in_party_affect_week_clus <- felm(in_party_affect ~ treatment | 
                                    election_year | 0 |election_year + week,
                                  data = ines_master,
                                  keepCX = TRUE)
summary(in_party_affect_week_clus)


out_party_affect_week_clus <- felm(party_affect ~ treatment | 
                                     election_year | 0 |election_year + week,
                                   data = ines_master,
                                   keepCX = TRUE)
summary(out_party_affect_no_clus)

## Create Table  

stargazer(base_week_clus, demo_week_clus, social_week_clus, 
          in_party_affect_week_clus, out_party_affect_week_clus,
          out= "Tables/main_election_week_clus.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization and Party Affect Alternative Clustering",
          keep = "treatment",
          dep.var.labels = c("Polarization", 
                             "In-Party Affect", "Out-Party Affect"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:elec_week_clus",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "Yes", "Yes", "No", "No"),
            c("Social Controls",  "No", "No", "Yes", "No", "No"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes" ),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes"),
            c("Week Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes")))


##############################################
# Create Table A13
##############################################

#### Models with wild cluster boot strap 

# Base model
ines_master <- ines_master %>%
  mutate(year = factor(election_year, levels = c(2001, 2003, 2006, 2009, 2013, 2015, 2019)))

base_model_wild <- lm(party_polar ~ treatment + year, data = ines_master)

base_wild_cluster <- cluster.boot(base_model_wild, ines_master$election_year, parallel = FALSE, use_white = NULL, 
                                  force_posdef = FALSE, R = 300, boot_type = "wild", 
                                  wild_type = "rademacher", debug = FALSE)

a<- coeftest(base_model_wild , base_wild_cluster)

# add demog
demo_model_wild <- lm(party_polar ~ treatment + age +
                        Sex + ashkenazi + year, 
                      data = ines_master)

demo_wild_cluster <- cluster.boot(demo_model_wild, ines_master$election_year, parallel = FALSE, use_white = NULL, 
                                  force_posdef = FALSE, R = 300, boot_type = "wild", 
                                  wild_type = "rademacher", debug = FALSE)

b<- coeftest(demo_model_wild , demo_wild_cluster)

# add social
social_model_wild <- lm(party_polar ~ treatment + age +
                          Sex + ashkenazi + relig + edu + year, 
                        data = ines_master)

social_wild_cluster <- cluster.boot(social_model_wild, ines_master$election_year, parallel = FALSE, use_white = NULL, 
                                    force_posdef = FALSE, R = 300, boot_type = "wild", 
                                    wild_type = "rademacher", debug = FALSE)

c <- coeftest(social_model_wild , social_wild_cluster)

# In party

in_party_wild_model <- lm(in_party_affect ~ treatment + year, data = ines_master)

in_party_wild_cluster <- cluster.boot(in_party_wild_model, ines_master$election_year, parallel = FALSE, use_white = NULL, 
                                      force_posdef = FALSE, R = 300, boot_type = "wild", 
                                      wild_type = "rademacher", debug = FALSE)

d<- coeftest(in_party_wild_model , in_party_wild_cluster)

# Out party

out_party_wild_model <- lm(party_affect ~ treatment + year, data = ines_master)

out_party_wild_cluster <- cluster.boot(out_party_wild_model, ines_master$election_year, parallel = FALSE, use_white = NULL, 
                                       force_posdef = FALSE, R = 300, boot_type = "wild", 
                                       wild_type = "rademacher", debug = FALSE)

e<- coeftest(out_party_wild_model , out_party_wild_cluster)


# Table
stargazer(a,b,c,d,e,
          out= "Tables/main_election_wild_clus.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization and Party Affect Wild Cluster Bootstrap",
          keep = "treatment",
          #dep.var.labels = c("Polarization", 
          #                  "In-Party Affect", "Out-Party Affect"),
          column.labels=c("Polarization",
                          "In-Party Affect", "Out-Party Affect"),
          column.separate = c(3,1,1),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:elec_wild_clus",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "No", "Yes", "Yes", "No", "No"),
            c("Social Controls",  "No", "No", "Yes", "No", "No"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes" ),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes", "Yes"),
            c("Wild Cluster Bootstrap",  "Yes", "Yes", "Yes", "Yes", "Yes")))


##############################################
# Create Table A14
##############################################
#Run models on same observations:
ines_new<- 
  ines_master %>% filter(!is.na(party_affect + in_party_affect + party_polar))

# Run base model + dem 
base_polar_rob_dem <- felm(party_polar ~ treatment + age +
                             Sex + ashkenazi + relig + edu | election_year | 0 |election_year,
                           data = ines_new,
                           keepCX = TRUE) 
summary(base_polar_rob_dem)

# Effect on in-party affect
in_party_rob <- felm(in_party_affect ~ treatment  + age +
                       Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                     data = ines_new,
                     keepCX = TRUE)
summary(in_party_rob)

#Effect on out-party affect
out_party_rob <- felm(party_affect ~ treatment  + age +
                        Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                      data = ines_new,
                      keepCX = TRUE)
summary(out_party_rob)

## Create Table  

stargazer(base_polar_rob_dem, in_party_rob, out_party_rob,
          out= "Tables/rob_same_pool.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization and Party Affect omitting Respondents where Polarization = NA",
          keep = "treatment",
          dep.var.labels = c("Polarization", 
                             "In-Party Affect", "Out-Party Affect"),
          covariate.labels = c("Proximity to Election"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:main_elec_rob_pool",
          star.char = c("", "", ""),
          add.lines = list(
            c("Demographic Controls",  "Yes", "Yes", "Yes"),
            c("Social Controls",  "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))


##############################################
# Create Table A15
##############################################

#effect of turnout on party_polar

turnout_pol<-felm(party_polar~turnout, 
                  data = ines_master,
                  exactDOF=TRUE,
                  keepCX = TRUE)
summary(turnout_pol)

#effect of turnout on party_polar + controls
turnout_pol_control<-felm(party_polar~turnout + age +
                            Sex + ashkenazi + relig + 
                            edu, data = ines_master,
                          exactDOF=TRUE,
                          keepCX = TRUE)
summary(turnout_pol_control)

#effect of turnout on party_polar + FEs
turnout_pol_control_FE<-felm(party_polar~turnout + age +
                               Sex + ashkenazi + relig + 
                               edu  | election_year | 0 |election_year, 
                             data = ines_master,
                             exactDOF=TRUE,
                             keepCX = TRUE)
summary(turnout_pol_control_FE)


stargazer(turnout_pol,turnout_pol_control, 
          turnout_pol_control_FE,
          out= "Tables/turnout_ef.tex",  style="qje",
          title="Effect of Turnout on Affective Polarization",
          keep = c("turnout"),
          dep.var.labels = c("Polarization"),
          covariate.labels = c("Turnout"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:turnout_ef",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "Yes", "Yes"),
            c("Social Controls",  "No", "Yes", "Yes"),
            c("Year FEs", "No", "No", "Yes")))


##############################################
# Create Table A16
##############################################

#Treatment effect conditional on right-wing

base_right <- felm(party_polar ~ treatment*right + age +
                     Sex + ashkenazi + relig + edu| election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(base_right)

#Treatment effect conditional on left-wing

base_left <- felm(party_polar ~ treatment*left+ age +
                    Sex + ashkenazi + relig + edu | election_year | 0 |election_year,
                  data = ines_master,
                  exactDOF=TRUE,
                  keepCX = TRUE) 
summary(base_left)

# Create table of HTE

stargazer(base_right, base_left,
          out= "Tables/HTE_ines_main.tex",  style="qje",
          title="Effect of Proximity to Election on Polarization Conditional on Ideology",
          dep.var.labels = c("Polarization"),
          keep = c("treatment", "right", "left"),
          #omit = c(3:31),
          covariate.labels = c("Proximity to election",
                               "Ideological Right", "Ideological Left", 
                               "Proximity to election*Right",
                               "Proximity to election*Left"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          no.space = T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          font.size = "small",
          label = "tab:hte_ines_main",
          star.char = c("", ""),
          add.lines = list(
            c("Demographic controls", "Yes", "Yes"),
            c("Social controls", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes")))


##############################################
# Create Table A17
##############################################

#Treatment effect on affect towards Shas

base_shas <- felm(uo_party_affect ~ treatment | election_year | 0 |election_year,
                  data = ines_master,
                  exactDOF=TRUE,
                  keepCX = TRUE) 
summary(base_shas)

#Treatment effect on affect towards Shas (conditional on right)

base_shas_right <- felm(uo_party_affect ~ treatment*right + age +
                          Sex + ashkenazi + relig + edu | election_year | 0 |election_year,
                        data = ines_master,
                        exactDOF=TRUE,
                        keepCX = TRUE) 
summary(base_shas_right)

#Treatment effect on affect towards Shas (conditional on left)

base_shas_left <- felm(uo_party_affect ~ treatment*left + age +
                         Sex + ashkenazi + relig + edu | election_year | 0 |election_year,
                       data = ines_master,
                       exactDOF=TRUE,
                       keepCX = TRUE) 
summary(base_shas_left)

#Treatment effect on affect towards Shas (conditional on center)

base_shas_center <- felm(uo_party_affect ~ treatment*center + age +
                           Sex + ashkenazi + relig + edu | election_year | 0 |election_year,
                         data = ines_master,
                         exactDOF=TRUE,
                         keepCX = TRUE) 
summary(base_shas_center)


# Create table of HTE

stargazer(base_shas, base_shas_right, base_shas_left, base_shas_center,
          out= "Tables/HTE_ines_shas.tex",  style="qje",
          title="Effect of Proximity to Election on Affect towards Shas party
          Conditional on Ideology",
          dep.var.labels = c("Affect towards Shas"),
          keep = c("treatment", "right", "left","center"),
          #omit = c(3:31),
          covariate.labels = c("Proximity to election",
                               "Ideological Right","Ideological Left", 
                               "Ideological Center",
                               "Proximity to election*Right",
                               "Proximity to election*Left", 
                               "Proximity to election*Center"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          ci=FALSE,
          no.space = T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          font.size = "small",
          label = "tab:hte_ines_shas",
          star.char = c("", ""),
          add.lines = list(
            c("Demographic controls", "No", "Yes", "Yes", "Yes"),
            c("Social controls", "No", "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes", "Yes")))



##############################################
# Create Table A18
##############################################
#add to appendix table that interacts treatment with a education, sex, age and ashkenazi. 
ines_master <- ines_master %>% 
  mutate(.,
         num_edu = case_when(
           edu == "Academic" ~ 3,
           edu == "HS" ~ 2,
           edu == "Less then HS" ~ 1
         ))

knowl_new_nocontrols  <- felm(knowledge ~ treatment*num_edu 
                              | election_year | 0 |election_year,
                              data = ines_master,
                              exactDOF=TRUE,
                              keepCX = TRUE) 
summary(knowl_new_nocontrols)

knowl_new_controls  <- felm(knowledge ~ treatment*num_edu 
                            + ashkenazi + Sex 
                            + age + relig
                            | election_year | 0 |election_year,
                            data = ines_master,
                            exactDOF=TRUE,
                            keepCX = TRUE) 
summary(knowl_new_controls)


stargazer(knowl_new_nocontrols,knowl_new_controls,
          out= "Tables/knowledge_moderate.tex",  style="qje",
          title="Effects of Competition on Knowledge, Moderated by Education",
          keep = c("treatment", "num_edu", "treatment*num_edu"),
          dep.var.labels = c("Success in Factual Question"),
          covariate.labels = c("Proximity to Election", "Education", "Proximity to Election*Education"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:knowledge_moderate",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "No", "Yes"),
            c("Social Controls",  "No", "Yes"),
            c("Year FEs", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes")))


##############################################
# Create Table A19
##############################################
knowl_age <- felm(knowledge ~ treatment*age +
                    Sex + ashkenazi + relig + edu
                  | election_year | 0 |election_year,
                  data = ines_master,
                  exactDOF=TRUE,
                  keepCX = TRUE) 
summary(knowl_age)

knowl_ethni <- felm(knowledge ~ treatment*ashkenazi + age +
                      Sex + relig + edu
                    | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(knowl_ethni)

knowl_gender <- felm(knowledge ~ treatment*Sex + age +
                       ashkenazi + relig + edu
                     | election_year | 0 |election_year,
                     data = ines_master,
                     exactDOF=TRUE,
                     keepCX = TRUE) 
summary(knowl_gender)


stargazer(knowl_age,knowl_ethni,knowl_gender,
          out= "Tables/moderation_knowledge.tex",  style="qje",
          title="Effects of Competition on Knowledge, Moderated by Age, Ethnicity and Gender",
          keep = c("treatment", "age", "ashkenazi", "Sex", "treatment*age", "treatment*ashkenazi",
                   "treatment*Sex"),
          dep.var.labels = c("Knowledge"),
          covariate.labels = c("Proximity to Election", "Age", "Ethnicity", "Gender", 
                               "Proximity to Election*Age", "Proximity to Election*Ethnicity",
                               "Proximity to Election*Gender"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:moderation_knowledge",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "Yes", "Yes", "Yes"),
            c("Social Controls",  "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))


##############################################
# Create Table A20
##############################################
conv_age <- felm(politc_conv ~ treatment*age +
                   Sex + ashkenazi + relig + edu
                 | election_year | 0 |election_year,
                 data = ines_master,
                 exactDOF=TRUE,
                 keepCX = TRUE) 
summary(conv_age)

conv_ethni <- felm(politc_conv ~ treatment*ashkenazi + age +
                     Sex + relig + edu
                   | election_year | 0 |election_year,
                   data = ines_master,
                   exactDOF=TRUE,
                   keepCX = TRUE) 
summary(conv_ethni)

conv_gender <- felm(politc_conv ~ treatment*Sex + age +
                      ashkenazi + relig + edu
                    | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(conv_gender)

stargazer(conv_age,conv_ethni,conv_gender,
          out= "Tables/moderation_engagement.tex",  style="qje",
          title="Effects of Competition on Engagement, Moderated by Age, Ethnicity and Gender",
          keep = c("treatment", "age", "ashkenazi", "Sex", "treatment*age", "treatment*ashkenazi",
                   "treatment*Sex"),
          dep.var.labels = c("Engagement"),
          covariate.labels = c("Proximity to Election", "Age", "Ethnicity", "Gender", 
                               "Proximity to Election*Age", "Proximity to Election*Ethnicity",
                               "Proximity to Election*Gender"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:moderation_engagement",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))



##############################################
# Create Table A21
##############################################

turnout_age <- felm(turnout ~ treatment*age +
                      Sex + ashkenazi + relig + edu
                    | election_year | 0 |election_year,
                    data = ines_master,
                    exactDOF=TRUE,
                    keepCX = TRUE) 
summary(turnout_age)

turnout_ethni <- felm(turnout ~ treatment*ashkenazi + age +
                        Sex + relig + edu
                      | election_year | 0 |election_year,
                      data = ines_master,
                      exactDOF=TRUE,
                      keepCX = TRUE) 
summary(turnout_ethni)

turnout_gender <- felm(turnout ~ treatment*Sex + age +
                         ashkenazi + relig + edu
                       | election_year | 0 |election_year,
                       data = ines_master,
                       exactDOF=TRUE,
                       keepCX = TRUE) 
summary(turnout_gender)


stargazer(turnout_age,turnout_ethni,turnout_gender,
          out= "Tables/moderation_turnout.tex",  style="qje",
          title="Effects of Competition on Turnout, Moderated by Age, Ethnicity and Gender",
          keep = c("treatment", "age", "ashkenazi", "Sex", "treatment*age", "treatment*ashkenazi",
                   "treatment*Sex"),
          dep.var.labels = c("Turnout"),
          covariate.labels = c("Proximity to Election", "Age", "Ethnicity", "Gender", 
                               "Proximity to Election*Age", "Proximity to Election*Ethnicity",
                               "Proximity to Election*Gender"),
          omit.stat = c("rsq", "f", "ser", "adj.rsq"),
          digits=4,
          ci=FALSE,
          font.size = "small",
          no.space= T,
          omit.table.layout = "n",
          star.cutoffs = NULL,
          label = "tab:moderation_turnout",
          star.char = c("", "", ""),
          add.lines = list(
            c("",  "", "", ""),
            c("Demographic Controls",  "Yes", "Yes", "Yes"),
            c("Social Controls",  "Yes", "Yes", "Yes"),
            c("Year FEs", "Yes", "Yes", "Yes"),
            c("Year Cluster",  "Yes", "Yes", "Yes")))



